<template>
  <div class="container">
    <el-card>
      <div v-if="$route.query.id">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item
            :to="{ path: '/subjects/list' }"
            id="subMangerDesign"
            >学科管理</el-breadcrumb-item
          >
          <el-breadcrumb-item>{{ $route.query.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
      </div>
      <!-- 表单 -->
      <el-form inline ref="tagForm" :model="tagForm" label-width="80px">
        <!-- 关键字 -->
        <el-form-item label="标签名称">
          <el-input v-model="tagForm.tagName"></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="tagForm.state" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="clearHandler">清除</el-button>
          <el-button @click="handleFilter" type="primary">搜索</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-link
            type="primary"
            :underline="false"
            class="el-button el-button--text el-button--medium"
            v-if="$route.query.id"
            @click="$router.back()"
          >
            <i class="el-icon-back"></i>
            返回学科
          </el-link>
          <el-button @click="addHandler" type="success"
            ><i class="el-icon-edit"></i>新增标签</el-button
          >
        </el-form-item>
      </el-form>

      <!-- 警告 -->
      <template>
        <el-alert
          :title="`数据一共${counts}条`"
          :closable="false"
          type="info"
          show-icon
        >
        </el-alert>
      </template>

      <!-- 表格 -->
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="80">
            <template v-slot="scope">
              {{ scope.$index + (tagForm.page - 1) * tagForm.pagesize + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科">
          </el-table-column>
          <el-table-column prop="tagName" label="标签名称"> </el-table-column>
          <el-table-column prop="username" label="创建人"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期">
            <template slot-scope="scope">
              {{ scope.row.addDate | forMat('YYYY-MM-DD hh:mm:ss') }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 1">启用</span>
              <span v-else>禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button @click="handleStatus(scope.row)" type="text">
                {{ scope.row.state ? '禁用' : '启用' }}
              </el-button>
              <el-button
                @click="edit(scope.row)"
                :disabled="!scope.row.state"
                type="text"
                >修改</el-button
              >
              <el-button
                @click="removeHandler(scope.row)"
                :disabled="!scope.row.state"
                type="text"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>

      <!-- 新增标签的对话框 -->
      <el-dialog
        title="新增目录"
        :visible.sync="addDialogVisible"
        width="500px"
      >
        <tags-add-dialog
          v-if="addDialogVisible"
          :row="row"
          :isEdit="isEdit"
          @success="successHandler"
        ></tags-add-dialog>
      </el-dialog>

      <!-- 分页 -->
      <template class="el-pagination">
        <div class="block">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="sform.page"
            :page-size="sform.pagesize"
            :page-sizes="[2, 5, 10, 20]"
            layout="prev, pager, next, sizes, jumper"
            :total="counts"
          >
          </el-pagination>
        </div>
      </template>
    </el-card>
  </div>
</template>

<script>
// import { status } from '@/api/hmmm/constants'
import { list, changeState, remove } from '@/api/hmmm/tags'
import tagsAddDialog from './tagsAddDialog'
export default {
  components: {
    'tags-add-dialog': tagsAddDialog
  },
  data () {
    return {
      // form 表单数据
      tagForm: {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 5,
        subjectID: null
      },
      // 第一次发请求的对象
      sform: {
        page: 1,
        pagesize: 5
      },
      // 表格数据
      tableData: [],
      // 数据总条数
      counts: 1,
      // 新增的对话框
      addDialogVisible: false,
      // 点击的是修改还是添加
      isEdit: false,
      row: {}
    }
  },
  async created () {
    if (this.$route.query.name) {
      this.tagForm.subjectID = this.$route.query.id
      this.TagsList()
    } else {
      // 获取列表
      this.getTagsList()
      // 重置 tagForm
      this.reset()
    }
  },
  methods: {
    reset () {
      this.tagForm = {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 5
      }
    },
    // 发送请求，获取标签的列表
    async getTagsList () {
      const res = await list(this.sform)
      this.tableData = res.data.items
      this.counts = res.data.counts
    },
    // 带状态的标签列表
    async TagsList () {
      const res = await list(this.tagForm)
      this.tableData = res.data.items
      this.counts = res.data.counts
    },
    // 搜索
    async handleFilter () {
      // this.sform = this.tagForm
      this.TagsList()
    },
    // 清除 表单
    clearHandler () {
      this.reset()
    },
    // 禁用，启用
    async handleStatus (row) {
      var status = ''
      if (row.state === 1) {
        row.state = 0
        status = '禁用'
      } else {
        row.state = 1
        status = '启用'
      }
      const data = {
        id: row.id,
        state: row.state
      }
      const res = await changeState(data)
      if (res.data.success) {
        const index = this.tableData.findIndex((item) => item.id === row.id)
        this.tableData[index].state = row.state ? 1 : 0
      }
      this.$message.success('已' + status)
    },
    // 新增标签
    addHandler () {
      this.addDialogVisible = true
      this.isEdit = false
    },
    // 新增标签和修改成功的回调函数
    successHandler () {
      this.addDialogVisible = false
      this.getTagsList()
    },
    // 修改
    edit (val) {
      // console.log(val, 'row')
      this.addDialogVisible = true
      this.isEdit = true
      this.row = val
    },
    // 删除
    removeHandler (row) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发送请求，删除
          await remove(row)
          if (this.tagForm.page > 1 && this.tableData.length === 1) {
            this.tagForm.page--
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 刷新列表
          this.getTagsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 分页
    async handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.sform.pagesize = val
      this.getTagsList()
    },
    async handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.sform.page = val
      this.getTagsList()
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  padding: 10px;
  .el-pagination {
    margin-top: 20px;
    text-align: right;
  }
}
</style>
<style>
#subMangerDesign {
  color: #606266 !important;
}
.el-breadcrumb__inner.is-link {
  color: #606266 !important;
}
</style>
